package com.boatAnchorAssistant.pojo.validation;

import com.boatAnchorAssistant.pojo.valid.Password;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import org.springframework.util.StringUtils;

/**
 * 【描 述】：
 * 【环 境】：J2SE 17
 * 【详 细】：
 *
 * @author 李松林
 * @version v1.0 2023/11/24 新建
 * @since 2023/11/24
 */
public class PasswordValidation implements ConstraintValidator<Password, String> {



    // 添加一个字段来存储正则表达式
    private String pattern;


    @Override
    public void initialize(Password constraintAnnotation) {
        // 从注解中读取正则表达式
        this.pattern = constraintAnnotation.pattern();
    }

    @Override
    public boolean isValid(String nameField, ConstraintValidatorContext cxt) {
        if (!StringUtils.hasText(nameField)) {
            // we can return true because the @NotNull annotation will be used for null checking
            return true;
        }
        // add your own name validation here
        return nameField.matches(pattern);
    }
}
